package cn.hackcloud.designpattern.singleton;

public class SingleTonTest {
	public static void main(String[] args) {
		SingleTon1 singleTon1 = SingleTon1.getInstance();
		SingleTon1 singleTon2 = SingleTon1.getInstance();
		System.err.println(singleTon1 == singleTon2);
		System.err.println(singleTon1.hashCode());
		System.err.println(singleTon2.hashCode());

	}
}

class SingleTon1 {
	private SingleTon1() {
	};

	private final static SingleTon1 instance = new SingleTon1();

	public static SingleTon1 getInstance() {
		return instance;
	}
}

class SingleTon2 {
	private SingleTon2() {
	};

	private static SingleTon2 instance;
	static {
		instance = new SingleTon2();
	}

	public static SingleTon2 getInstance() {
		return instance;
	}
}

class SingleTon3 {
	private SingleTon3() {
	};

	private static SingleTon3 instance;

	public static synchronized SingleTon3 getInstance() {
		if (instance == null) {
			instance = new SingleTon3();
		}
		return instance;
	}
}

class SingleTon4 {
	private SingleTon4() {
	};

	private static volatile SingleTon4 instance;

	public static SingleTon4 getInstance() {
		if (instance == null) {
			synchronized (SingleTon4.class) {
				if (instance == null) {
					instance = new SingleTon4();
				}
			}
		}
		return instance;
	}
}
